﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DomenModel;
using OMS.Models;
using Infrastructure.Service;
using Model.Service;

namespace OMS.Controllers
{
    public class ItemController : Controller
    {
        private IItemService _service;

        public ItemController(IItemService _service)
        {
            this._service = _service;
        }

        public ActionResult Index()
        {
            ViewBag.currentPage = 1;
            ViewBag.itemOnPage = 10;
            var x = _service.GetPaginationListOfItems(1, ViewBag.itemOnPage, null);
            ViewBag.Items = _service.GetPaginationListOfItems(1, ViewBag.itemOnPage, null);
            ViewBag.ItemsCount = _service.GetCountOfItems();

            if (ViewBag.ItemsCount % ViewBag.itemOnPage != 0) ViewBag.lastPage = ViewBag.ItemsCount / ViewBag.itemOnPage + 1;
            else ViewBag.lastPage = ViewBag.ItemsCount / ViewBag.itemOnPage;


            return View();
        }
        


        public ActionResult ItemTable(int? page, int? itemOnPage, string orderItemsBy, string searchString, string listSelectField, string listSelectCondition)
        {
            ViewBag.NameSortParm = String.IsNullOrEmpty(orderItemsBy) ? "ItemName desc" : "";
            ViewBag.SortByPrice = orderItemsBy == "Price" ? "Price desc" : "Price";
            ViewBag.SortByDescription = orderItemsBy == "Description" ? "Description desc" : "Description";

            


            int currentPage = page ?? 1;
            ViewBag.itemOnPage = itemOnPage ?? 10;
            
                if (String.IsNullOrWhiteSpace(searchString))
                {
                     ViewBag.Items = _service.GetPaginationListOfItems(currentPage, ViewBag.itemOnPage, orderItemsBy);
                     
                    ViewBag.ItemsCount = _service.GetCountOfItems();
                }
                else
                {
                    ViewBag.Items = _service.GetPaginationWithFilter(currentPage, ViewBag.itemOnPage, orderItemsBy, searchString, listSelectField, listSelectCondition);
                    
                    ViewBag.ItemsCount = _service.GetCountOfItems(searchString, listSelectField, listSelectCondition);
                }
            

            if (ViewBag.ItemsCount % ViewBag.itemOnPage != 0) ViewBag.lastPage = ViewBag.ItemsCount / ViewBag.itemOnPage + 1;
            else ViewBag.lastPage = ViewBag.ItemsCount / ViewBag.itemOnPage;

            ViewBag.currentPage = currentPage;
            ViewBag.currentSortItemsBy = orderItemsBy;
            ViewBag.searchString = searchString;
            ViewBag.listSelectField = listSelectField;
            ViewBag.listSelectCondition = listSelectCondition;


            return PartialView();
        }





        

        


        public ActionResult Details(int id)
        {
            return View();
        }

    

        public ActionResult CreateItem()
        {

            return View();
        }

        [HttpPost]
        public ActionResult CreateItem(CreateItem model)
        {
            if (ModelState.IsValid)
            {
                Item item = new Item
                {
                    Name = model.ItemName,
                    Description = model.ItemDescription,
                    ItemPrice = model.ItemPrice,
                    IsActive = true,
                };   

               
                _service.CreateItem(item);
                return RedirectToAction("Index");
            }
            else
            {
                ModelState.AddModelError("", "Item is not valid");
            }

            return View(model);
        }

        public ActionResult CreateItemDimension(int id)
        {
                        
            ViewBag.Id = id;
             ViewBag.Name = _service.GetItemById(id).Name;
          
            return View();
        }

     
        [HttpPost]
        public ActionResult CreateItemDimension(ItemDimension collection)
        {
            try
            {
               
               
                _service.AddNewItemDimension(collection);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }




        public ActionResult EditItemDimension(int id)
        {
            try
            {
                ViewBag.Id = id;
                _service.EditItemDimension(_service.GetItemDimensionById(id));
                
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }

        }

        [HttpGet]
        public ActionResult Dublicate(string itemName)
        {
            var item = _service.GetItem(itemName);
            CreateItem model = new CreateItem();
            model.ItemName = item.Name;
             model.ItemDescription = item.Description;
             model.ItemPrice = item.ItemPrice;
                    

            return View(model);
        }

        [HttpPost]
        public ActionResult Dublicate(CreateItem model)
        {
            bool statusOfSafe;
            if (ModelState.IsValid)
            {
                Item item = new Item
                {
                    Name = model.ItemName,
                    Description = model.ItemDescription,
                    ItemPrice = model.ItemPrice,
                    IsActive = true,
                    
                };

                statusOfSafe = _service.CreateItem(item);
                if (statusOfSafe == true) ModelState.AddModelError("", "User are create");
            }
            else
            {
                ModelState.AddModelError("", "User not valid");
            }


            return View(model);
        }

      

        [HttpPost]
        public ActionResult EditItemDimension(int id, ItemDimension collection)
        {
            try
            {
               
               
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
       

        public ActionResult EditItem(int id)
        {
            ViewBag.Id = id;
           
            Item item = _service.GetItemById(id);

            return View(item);
        }

        [HttpPost]
        public ActionResult EditItem(int id, Item item)
        {
            try
            {
            ViewBag.Id = id;
           
                _service.EditItem(item);
               
            
            
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


        public ActionResult Delete(int id)
        {
          
            Item item = _service.GetItemById(id);
            return View(item);
        }

        

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                        
                _service.DeactivateItemById(id);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public JsonResult CheckItemName(string ItemName)
        {
           
            Item item = _service.GetItem(ItemName);

            if (item == null)
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }
            return Json(false, JsonRequestBehavior.AllowGet);

        }


     
    }
}
